﻿Imports TMySQL
Public Class frmLogin
    Private _ConnectList As TMyConnectList
    Private _ModalResult As Boolean
    Private _Connection As TMySession
    Private Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub

    Public Sub New(ByVal AConnection As TMySession)
        Me.New()
        _Connection = AConnection
        _ConnectList = New TMyConnectList
    End Sub

    Private Sub InitFrm()
        Cursor.Current = Cursors.WaitCursor
        lv.BeginUpdate()
        lv.Items.Clear()
        Try
            For i As Integer = 0 To _ConnectList.Count - 1
                Dim li As New ListViewItem
                li.Text = _ConnectList(i).UserName
                li.SubItems.Add(_ConnectList(i).DatabaseName)
                li.SubItems.Add(_ConnectList(i).HostName)
                lv.Items.Add(li)
            Next
            For Each ch As ColumnHeader In lv.Columns
                ch.Width = -2
            Next ch
        Finally
            lv.EndUpdate()
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Public Function ShowEx() As Boolean
        _ModalResult = False
        InitFrm()
        Me.ShowDialog()
        Return _ModalResult
    End Function

    Private Sub frmLogin_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Me.Activate()
    End Sub

    Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
        Try
            Me.Login()
            _ModalResult = True
            If chkAddList.Checked Then
                _ConnectList.AddItem(txtUserName.Text, txtPassword.Text, txtDatabase.Text, txtHostName.Text)
                _ConnectList.Save()
            End If
            Me.Close()
        Catch ex As Exception
            Call MsgBox("接続に失敗しました" & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub Login()
        _Connection.UserName = txtUserName.Text
        _Connection.Password = txtPassword.Text
        _Connection.HostName = txtHostName.Text
        _Connection.DatabaseName = txtDatabase.Text
        _Connection.Connected = True
    End Sub

    Private Sub cmdQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdQuit.Click
        _ModalResult = False
        Me.Close()
    End Sub

    Private Sub lv_ItemSelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lv.ItemSelectionChanged
        If e.ItemIndex >= 0 Then
            txtUserName.Text = _ConnectList(e.ItemIndex).UserName
            txtPassword.Text = _ConnectList(e.ItemIndex).Password
            txtDatabase.Text = _ConnectList(e.ItemIndex).DatabaseName
            txtHostName.Text = _ConnectList(e.ItemIndex).HostName
        End If
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        If lv.SelectedItems.Count <= 0 Then
            Return
        End If
        Cursor.Current = Cursors.WaitCursor
        Try
            _ConnectList.DeleteItem(lv.SelectedItems(0).Index)
            InitFrm()
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub cmdUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUp.Click
        If lv.SelectedItems.Count <= 0 Then
            Return
        End If
        Cursor.Current = Cursors.WaitCursor
        Try
            Dim idx As Integer = lv.SelectedItems(0).Index
            _ConnectList.Up(lv.SelectedItems(0).Index)
            InitFrm()
            lv.Items(idx - 1).Selected = True
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub cmdDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDown.Click
        If lv.SelectedItems.Count <= 0 Then
            Return
        End If
        Cursor.Current = Cursors.WaitCursor
        Try
            Dim idx As Integer = lv.SelectedItems(0).Index
            _ConnectList.Down(idx)
            InitFrm()
            lv.Items(idx + 1).Selected = True
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub
End Class